package com.cooper.scraper;

import android.util.Log;

import com.cooper.android.comm.Resource;
import com.cooper.scraper.DownloadException;
import com.cooper.scraper.ResourceScraper;
import com.cooper.scraper.ResultUrl;
import com.cooper.scraper.ScraperException;

public class BrowseResourceCollector<T extends Resource> extends ResourceCollector<T> {
	
	private static final String BROWSE_LIST_KEY = "BROWSE_LIST_";

	@Override
	public String generateKey() {
		return BROWSE_LIST_KEY  + getPageNumber();
	}

	@Override
	public ResultUrl scraperListUrl(ResourceScraper<T> scraper)
			throws ScraperException, DownloadException {
		return scraper.createBrowseUrl(getPageNumber());
	}

	@Override
	public void scraperResult(ResourceScraper<T> scraper, String page)
			throws ScraperException, DownloadException {
		medias = scraper.extractBrowseResource(page);
		Log.d("BrowseResultMedias","count:" + medias.size());
		setPageCount(scraper.getBrowsePageCount(page));
	}

	@Override
	public boolean subscribed(String key) {
		return key.startsWith(BROWSE_LIST_KEY);
	}

	@Override
	public void scraperResult(ResourceScraper<T> scraper, String page,
			String redirectUrl) throws ScraperException, DownloadException {
		medias = scraper.extractBrowseResource(page,redirectUrl);
		Log.d("BrowseResultMedias","count:" + medias.size());
		setPageCount(scraper.getBrowsePageCount(page,redirectUrl));
	}

}
